#include <bwio.h>
#include <ts7200.h>
#include <user.h>

void StackPrint( int len ) {
  int stack_ptr;
  asm volatile (
    "mov %0, sp\n\t"
    : "=r" (stack_ptr)
  );
  int i, addr = stack_ptr + len*0x4;
  for ( i = 0; i < len; i ++ ) {
    addr -= 0x4;
    int *stack = (int*)( addr );
    bwprintf( COM2, "[ 0x%x ]\t", addr );
    bwprintf( COM2, "0x%x ", *stack );
    bwprintf( COM2, "\r\n" );
  }
}

void UserTaskIdPrinter() {
  int id = MyTid();
  int parent = MyParentTid();
  bwprintf( COM2, "Id: %d\tParent: %d\r\n", id, parent );
  Pass();
  bwprintf( COM2, "Id: %d\tParent: %d\r\n", id, parent );
  Exit();
}

void UserInit() {
  int child;
  child = Create( 0, &UserTaskIdPrinter );
  bwprintf( COM2, "Created: %d\r\n", child );
  child = Create( 0, &UserTaskIdPrinter );
  bwprintf( COM2, "Created: %d\r\n", child );
  child = Create( 4, &UserTaskIdPrinter );
  bwprintf( COM2, "Created: %d\r\n", child );
  child = Create( 4, &UserTaskIdPrinter );
  bwprintf( COM2, "Created: %d\r\n", child );
  bwprintf( COM2, "First: exiting\r\n" );
  Exit();
}
